package pt.isec.android.MyHistory;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PaintNote extends Activity{
	private Paint mPaint;
	LinearLayout nv;
	MyView vista;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paintnote);
        
    	TextView x = new TextView(getApplicationContext());
    	x.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    	LinearLayout ll = (LinearLayout) findViewById(R.id.llTitle122);
		x.setText(getString(R.string.localmain) + ">" + getString(R.string.localaddnote) + ">" + getString(R.string.localpaintnote));
		ll.addView(x);
        
        nv = (LinearLayout) findViewById(R.id.ll122PaintNote);
        nv.setBackgroundColor(Color.WHITE);
        nv.setDrawingCacheEnabled(true);
        vista = new MyView(this);
        nv.addView(vista);
        
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3);
    }
	
	public void saveit(View v){
		TextView tx = new TextView(this);
		//nv.addView(tx);
		
		vista.setDrawingCacheEnabled(true);
		Bitmap b = vista.getDrawingCache();
		//EditText et = (EditText) findViewById(R.id.e);
		//TextView tv = (TextView) findViewById(R.id.e);
		
		try {
			b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/sdcard/Images/image.jpg"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	class Point {
    	float x, y;
    }

    public class MyView extends View {
    	List<Point> points = new ArrayList<Point>();
        
        float x;
        float y;
        
        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;
        
        GestureDetector gd;

        public MyView(Context context) {
            super(context);
            
            this.setBackgroundColor(Color.WHITE);
			gd = new GestureDetector(gl);
			this.setLongClickable(true);
            
            setFocusable(true);
            setFocusableInTouchMode(true);
            
            
            mPath = new Path();
        }
        
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(20, 20, oldw, oldh);
            mBitmap = Bitmap.createBitmap(20, 20, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        }
        
        @Override
        protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			for (Point point : points) {
                canvas.drawCircle(point.x, point.y, 1, mPaint);  
            }
			
            canvas.drawBitmap(mBitmap, 0, 0, mPaint);
            
            canvas.drawPath(mPath, mPaint);
            
            invalidate();
		}
        
        @Override
		public boolean onTouchEvent(MotionEvent event) {
        	x = event.getX();
            y = event.getY();
			if(gd.onTouchEvent(event))
				return true;
			
			return super.onTouchEvent(event);
		}
        
        OnGestureListener gl = new OnGestureListener() {

        	public boolean onDown(MotionEvent e) {
        		mPath.moveTo(x, y);
        		mX = x;
        		mY = y;
        		invalidate();
        		return true;
        	}

        	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
        		mPath.lineTo(mX, mY);
        		mCanvas.drawPath(mPath, mPaint);
        		invalidate();
        		return true;
        	}

        	public void onLongPress(MotionEvent e) {
        		// TODO Auto-generated method stub
			
        	}

        	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
					float distanceY) {
        		float dx = Math.abs(x - mX);
        		float dy = Math.abs(y - mY);
        		if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        			mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
        			mX = x;
        			mY = y;
        			invalidate();
        			return true;
        		}
        		invalidate();
        		return false;
			}

			public void onShowPress(MotionEvent e) {
				// TODO Auto-generated method stub
			}

			public boolean onSingleTapUp(MotionEvent e) {
				Point point = new Point();
				point.x = x;
				point.y = y;
				points.add(point);
				invalidate();
				return true;
			}
        };
    }
}
